home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 260_01 / rz.c < prev    next >
Text File  |  1988-02-23  |  34KB  |  1,413 lines

  1. #define VERSION "1.14 01-15-87"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc  -DNFGVMIN -DCRCTABLE -K -O -i % -o rz; size rz
  5.  *
  6.  * rz.c By Chuck Forsberg
  7.  *      MS-DOS version by Francois Bergeon
  8.  *
  9.  *      cc -O rz.c -o rz                USG (3.0) Unix
  10.  *      cc -O -DV7  rz.c -o rz          Unix V7, BSD 2.8 - 4.3
  11.  *      cl rz.c -link stty              MS-DOS, MS-C 4.00+
  12.  *
  13.  *      ln rz rb                        For USG or V7
  14.  *
  15.  *      ln rz /usr/bin/rzrmail          For remote mail.  Make this the
  16.  *                                      login shell. rzrmail then calls
  17.  *                                      rmail(1) to deliver mail.
  18.  *
  19.  *              define CRCTABLE to use table driven CRC
  20.  *
  21.  *  Unix is a trademark of Western Electric Company
  22.  *
  23.  * A program for Unix to receive files and commands from computers running
  24.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting XMODEM.
  25.  *  rz uses Unix buffered input to reduce wasted CPU time.
  26.  *
  27.  * Iff the program is invoked by rzCOMMAND, output is piped to 
  28.  * "COMMAND filename"
  29.  *
  30.  *  Some systems (Venix, Coherent, Regulus) may not support tty raw mode
  31.  *  read(2) the same way as Unix. ONEREAD must be defined to force one
  32.  *  character reads for these systems. Added 7-01-84 CAF
  33.  *
  34.  *  Alarm signal handling changed to work with 4.2 BSD 7-15-84 CAF 
  35.  *
  36.  *  NFGVMIN Added 1-13-85 CAF for PC-AT Xenix systems where c_cc[VMIN]
  37.  *  doesn't seem to work (even though it compiles without error!).
  38.  *
  39.  *  HOWMANY should be tuned for best performance
  40.  *
  41.  *  USG UNIX (3.0) ioctl conventions courtesy  Jeff Martin
  42.  *
  43.  *  MS-DOS Adaptation 10-19-87 for the MS-C compiler rev 4.00+
  44.  *  using Francois Bergeon's tty library
  45.  */
  46. #define LOGFILE "/tmp/rzlog"
  47.  
  48. #include <stdio.h>
  49. #include <signal.h>
  50. #include <setjmp.h>
  51. #include <ctype.h>
  52.  
  53. #ifdef MSDOS
  54. #include <time.h>
  55. #define CRCTABLE
  56. #define JAN1ST70  315532800
  57. #else
  58. FILE *popen();
  59. #endif
  60.  
  61. #define OK 0
  62. #define FALSE 0
  63. #define TRUE 1
  64. #define ERROR (-1)
  65.  
  66. /*
  67.  * Max value for HOWMANY is 255.
  68.  *   A larger value reduces system overhead but may evoke kernel bugs.
  69.  *   133 corresponds to a XMODEM/CRC sector
  70.  */
  71. #ifndef HOWMANY
  72. #define HOWMANY 133
  73. #endif
  74.  
  75. int Zmodem = 0;         /* ZMODEM protocol requested */
  76. int Nozmodem = 0;       /* If invoked as "rb" */
  77. unsigned Baudrate;
  78. #include "rbsb.c"       /* most of the system dependent stuff here */
  79.  
  80. char *substr();
  81. FILE *fout;
  82.  
  83. /*
  84.  * Routine to calculate the free bytes on the current file system
  85.  *  ~0 means many free bytes (unknown)
  86.  */
  87. long getfree()
  88.    {
  89.    return(~0L);    /* many free bytes ... */
  90.    }
  91.  
  92. /* Ward Christensen / CP/M parameters - Don't change these! */
  93. #define ENQ       005
  94. #define CAN       ('X'&037)
  95. #define XOFF      ('s'&037)
  96. #define XON       ('q'&037)
  97. #define SOH       1
  98. #define STX       2
  99. #define EOT       4
  100. #define ACK       6
  101. #define NAK       025
  102. #define CPMEOF    032
  103. #define WANTCRC   'C'     /* send C not NAK to get crc not checksum */
  104. #define TIMEOUT   (-2)
  105. #define RCDO      (-3)
  106. #define ERRORMAX  5
  107. #define RETRYMAX  5
  108. #define WCEOT     (-10)
  109. #define SECSIZ    128     /* cp/m's Magic Number record size */
  110. #define PATHLEN   257     /* ready for 4.2 bsd ? */
  111. #define KSIZE     1024    /* record size with k option */
  112. #define UNIXFILE  0x8000  /* happens to the the S_IFREG file mask bit for stat */
  113.  
  114. int Lastrx;
  115. int Crcflg = FALSE;
  116. int Firstsec;
  117. int Eofseen;            /* indicates cpm eof (^Z) has been received */
  118. int errors;
  119. int Restricted = 0;     /* restricted; no /.. or ../ in filenames */
  120. #ifdef ONEREAD          /* Sorry, Regulus and some others don't */
  121. int Readnum = 1;        /* work right in raw mode! */
  122. #else                         
  123. int Readnum = HOWMANY;  /* Number of bytes to ask for in read() from modem */
  124. #endif
  125.  
  126. #define DEFBYTL 2000000000L   /* default rx file size */
  127. long Bytesleft;               /* number of bytes of incoming file left */
  128. long Modtime;                 /* Unix style mod time for incoming file */
  129. short Filemode;               /* Unix style mode for incoming file */
  130. char Pathname[PATHLEN];
  131. char *Progname;               /* the name by which we were called */
  132.  
  133. int Batch = 0;
  134. int Wcsmask = 0377;
  135. int Topipe = 0;
  136. int MakeLCPathname = TRUE; /* make received pathname lower case */
  137. int Verbose = 0;
  138. int Quiet = 0;             /* overrides logic that would otherwise set verbose */
  139. int Nflag = 0;             /* Don't really transfer files */
  140. int Rxbinary = FALSE;      /* receive all files in bin mode */
  141. int Rxascii = FALSE;       /* receive files in ascii (translate) mode */
  142. int Thisbinary;            /* current file is to be received in bin mode */
  143. int Blklen;                /* record length of received packets */
  144. char secbuf[KSIZE];
  145. char linbuf[HOWMANY];
  146. int Lleft = 0;             /* number of characters in linbuf */
  147. time_t timep[2];
  148. char Lzmanag;              /* Local file management request */
  149. char zconv;                /* ZMODEM file conversion request */
  150. char zmanag;               /* ZMODEM file management request */
  151. char ztrans;               /* ZMODEM file transport request */
  152.  
  153. jmp_buf tohere;            /* For the interrupt on RX timeout */
  154.  
  155. #include "zm.c"
  156.  
  157. int tryzhdrtype = ZRINIT;  /* Header type to send corresponding to */
  158.                            /* Last rx close */
  159.  
  160. alrm()
  161.    {
  162.    longjmp(tohere, -1);
  163.    }
  164.  
  165. /* called by signal interrupt or terminate to clean things up */
  166. bibi(n)
  167.    {
  168.    if (Zmodem)
  169.       zmputs(Attn);
  170.    canit();
  171.    mode(0);
  172.    fprintf(stderr, "rz: caught signal %d; exiting\n", n);
  173.    exit(128+n);
  174.    }
  175.  
  176. main(argc, argv)
  177. char *argv[];
  178.    {
  179.    char *cp;
  180.    register npats;
  181.    char *virgin, **patts;
  182.    char *getenv();
  183.    int exitcode;
  184.  
  185.    Rxtimeout = 100;
  186.    setbuf(stderr, NULL);
  187.    if ((cp = getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  188.       Restricted = TRUE;
  189.  
  190. #ifndef MSDOS
  191.    chkinvok((virgin = argv[0]));       /* if called as [-]rzCOMMAND set flag */
  192. #else
  193.    Progname = virgin = "rz";
  194. #endif
  195.    npats = 0;
  196.    while (--argc)
  197.       {
  198.       cp = *++argv;
  199.       if (*cp++ == '-' && *cp)
  200.          {
  201.          while (*cp)
  202.             {
  203.             switch (*cp++)
  204.                {
  205.                case '+':
  206.                   Lzmanag = ZMAPND;
  207.                   break;
  208. #ifndef MSDOS
  209.                case '1':
  210.                   iofd = 1;
  211.                   break;
  212. #else
  213.                case '2':
  214.                   port = "COM2";
  215.                   break;
  216.                case 's':
  217.                   Baudrate = atoi(cp);
  218.                   if ((speed = getspeed(Baudrate)) < 0)
  219.                      usage();
  220.                   *cp = '\0';
  221.                   break;
  222.                case 'Y':
  223.                   Nozmodem = TRUE;
  224.                   break;
  225. #endif
  226.                case '7':
  227.                   Wcsmask = 0177;
  228.                case 'a':
  229.                   Rxascii = TRUE;
  230.                   break;
  231.                case 'b':
  232.                   Rxbinary = TRUE;
  233.                   break;
  234.                case 'c':
  235.                   Crcflg = TRUE;
  236.                   break;
  237.                case 'D':
  238.                   Nflag = TRUE;
  239.                   break;
  240.                case 'p':
  241.                   Lzmanag = ZMPROT;
  242.                   break;
  243.                case 'q':
  244.                   Quiet = TRUE;
  245.                   Verbose = 0;
  246.                   break;
  247.                case 't':
  248.                   if (--argc < 1)
  249.                      usage();
  250.                   Rxtimeout = atoi(*++argv);
  251.                   if (Rxtimeout < 10 || Rxtimeout > 1000)
  252.                      usage();
  253.                   break;
  254.                case 'u':
  255.                   MakeLCPathname = FALSE;
  256.                   break;
  257.                case 'v':
  258.                   ++Verbose;
  259.                   break;
  260.                default:
  261.                   usage();
  262.                }
  263.             }
  264.          }
  265.       else if (!npats && argc > 0)
  266.          {
  267.          if (argv[0][0])
  268.             {
  269.             npats = argc;
  270.             patts = argv;
  271. #ifde